﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BL.Entity;
using DL;
namespace BL.Service
{
    public class tbl_PeriodService : ServiceBase
    {
        public List<Period> GetPeriod()
        {
            var query = from item in this.Context.tbl_Period
                        select new Period()
                        {
                            Active = item.Active.Value,
                            CreateBy = item.CreateBy,
                            CreateDate = item.CreateDate.Value,
                            EndDate = item.EndDate,
                            StartDate = item.StartDate,
                            PeriodID = item.PeriodID,
                            UpdateBy = item.UpdateBy,
                            UpdateDate = item.UpdateDate.Value
                        };
            return query.ToList();
        }
        public void AddPeriod(Period dto)
        {
            if (!CheckCanUsePeriod(dto)) { throw new Exception("วันที่เริ่มต้นไม่สามารถใช้ได้"); }
            DateTime dateNow = GetDateGobal();

            tbl_Period addItem = new tbl_Period()
            {
                Active = false,
                EndDate = dto.EndDate,
                StartDate = dto.StartDate,
                UpdateDate = dateNow,
                UpdateBy = dto.UpdateBy,
                CreateBy = dto.CreateBy,
                CreateDate = dateNow,

            };
            this.Context.AddTotbl_Period(addItem);
            this.Context.SaveChanges();
        }
        public void UpdatePeriod(Period dto) {

            if (!CheckCanUsePeriod(dto)) { throw new Exception("วันที่เริ่มต้นไม่สามารถใช้ได้"); }
            DateTime dateNow = GetDateGobal();

            var query = from item in this.Context.tbl_Period
                        where item.PeriodID == dto.PeriodID
                        select item;
            tbl_Period updateItem = query.FirstOrDefault();
            updateItem.UpdateBy = dto.UpdateBy;
            updateItem.UpdateDate = dateNow;
            updateItem.StartDate = dto.StartDate;
            updateItem.EndDate = dto.EndDate;
            updateItem.Active = dto.Active;
            this.Context.SaveChanges();
        }
        public void DeletePerid(Period dto) {
            var query = from item in this.Context.tbl_Period
                        where item.PeriodID == dto.PeriodID
                        select item;
            tbl_Period deleteItem = query.FirstOrDefault();
            this.Context.DeleteObject(deleteItem);
            this.Context.SaveChanges();
        }
        public bool CheckCanUsePeriod(Period dto)
        {
            var query = from item in this.Context.tbl_Period
                        where item.PeriodID!=dto.PeriodID
                        select item;
            if (query.ToList().Count == 0) {
                return true;
            }
            return dto.StartDate > query.Max(s => s.EndDate);
        }
    }
}
